package com.wen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.common.Result;
import com.wen.mapper.ConsumerMapper;
import com.wen.pojo.Consumer;
import com.wen.pojo.dto.ConsumerDto;
import com.wen.pojo.dto.SupplierDto;
import com.wen.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private ConsumerMapper mapper;

    @Override
    public Result ConById(Long consumerId) {
        ConsumerDto consumerDto = mapper.ConById(consumerId);
        if(StringUtils.isEmpty(consumerDto)){
            return Result.fail("该供应商不存在");
        }
        return Result.ok(consumerDto);
    }

    @Override
    public Result views(Long consumerId) {
        SupplierDto views = mapper.views(consumerId);
        if(StringUtils.isEmpty(views)){
            return Result.fail("该供应商不存在");
        }
        return Result.ok(views);
    }

    @Override
    public Page<Consumer> queryByPage(ConsumerDto dto, Long pageNum, Long pageSize) {

        Page<Consumer> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getNickName())) {
            wrapper.like("nick_name", dto.getNickName());
        }
        if (!StringUtils.isEmpty(dto.getSex())){
            wrapper.like("sex",dto.getSex());
        }

        wrapper.eq("status","s001-1");

        return mapper.selectPage(page,wrapper);
    }
}
